package cn.iocoder.yudao.module.biz.controller.admin.contract;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.contract.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.contract.ContractArchiveDO;
import cn.iocoder.yudao.module.biz.service.contract.ContractArchiveService;

@Tag(name = "管理后台 - 合同档案")
@RestController
@RequestMapping("/san/biz-contract-archive")
@Validated
public class ContractArchiveController {

    @Resource
    private ContractArchiveService contractArchiveService;

    @PostMapping("/create")
    @Operation(summary = "创建合同档案")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:create')")
    public CommonResult<Long> createContractArchive(@Valid @RequestBody ContractArchiveSaveReqVO createReqVO) {
        return success(contractArchiveService.createContractArchive(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同档案")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:update')")
    public CommonResult<Boolean> updateContractArchive(@Valid @RequestBody ContractArchiveSaveReqVO updateReqVO) {
        contractArchiveService.updateContractArchive(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同档案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:delete')")
    public CommonResult<Boolean> deleteContractArchive(@RequestParam("id") Long id) {
        contractArchiveService.deleteContractArchive(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除合同档案")
                @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:delete')")
    public CommonResult<Boolean> deleteContractArchiveList(@RequestParam("ids") List<Long> ids) {
        contractArchiveService.deleteContractArchiveListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同档案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:query')")
    public CommonResult<ContractArchiveRespVO> getContractArchive(@RequestParam("id") Long id) {
        ContractArchiveDO contractArchive = contractArchiveService.getContractArchive(id);
        return success(BeanUtils.toBean(contractArchive, ContractArchiveRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同档案分页")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:query')")
    public CommonResult<PageResult<ContractArchiveRespVO>> getContractArchivePage(@Valid ContractArchivePageReqVO pageReqVO) {
        PageResult<ContractArchiveDO> pageResult = contractArchiveService.getContractArchivePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContractArchiveRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合同档案 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContractArchiveExcel(@Valid ContractArchivePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContractArchiveDO> list = contractArchiveService.getContractArchivePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合同档案.xls", "数据", ContractArchiveRespVO.class,
                        BeanUtils.toBean(list, ContractArchiveRespVO.class));
    }

    @PutMapping("/changeStatus")
    @Operation(summary = "变更合同状态")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:update')")
    public CommonResult<Boolean> changeStatus(@Valid @RequestBody ContractArchiveSaveReqVO updateReqVO) {
        // status = 0-草拟,1-待生效,2-生效,3-待续签,4结束,5流失,6无效
        contractArchiveService.changeStatus(updateReqVO);
        return success(true);
    }

    @PutMapping("/renew")
    @Operation(summary = "续签合同")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:update')")
    public CommonResult<Boolean> renewContractArchive(@Valid @RequestBody ContractArchiveSaveReqVO updateReqVO) {
        contractArchiveService.renewContractArchive(updateReqVO);
        return success(true);
    }

    @GetMapping("/getAutoGenerateCode")
    @Operation(summary = "获得合同档案自动生成的合同编号")
    @Parameter(name = "ruleCode", description = "规则代码", required = true, example = "test001")
    @PreAuthorize("@ss.hasPermission('san:biz-contract-archive:create')")
    public CommonResult<String> getAutoGenerateCode(@RequestParam("ruleCode") String ruleCode) {
        String code = contractArchiveService.getAutoGenerateCode(ruleCode);
        return success(code);
    }
}